// 碰撞检测
import { _decorator, Canvas, Collider, Component, Node, RigidBody, Size, SkeletalAnimation, UITransform, Vec3, view } from 'cc';
import { PlayerController } from '../controllers/PlayerController';
import { PlayerManager } from '../managers/PlayerManager';
import { RoadManager } from '../managers/RoadManager';

const { ccclass, property } = _decorator;
@ccclass('CollisionExample')
export class CollisionExample extends Component {
    private playerManager: PlayerManager;
    private skeletalAnimation: SkeletalAnimation;
    private playerController: PlayerController;

    mapParent: Node;
    digitalMarkup: number = 1;
    rigidBody: any;
    @property(Node)
    gameOver: Node = null;
    private animState: any;
    collider: Collider;
    bool: Boolean = false;
    roadManager: RoadManager;

    onLoad() {
        // 确保绑定了 Rigidbody 组件
        if (!this.rigidBody) {
            this.rigidBody = this.getComponent(RigidBody);
        }
        // 监听碰撞事件
        this.collider = this.getComponent(Collider);
        if (this.collider) {
            this.collider.on('onCollisionEnter', this.onCollisionEnter, this);
            this.collider.on('onCollisionExit', this.onCollisionEnd, this);
        }
    }

    start(): void {
        this.playerController = this.getComponent(PlayerController);
        this.playerManager = PlayerManager.getInstance();
        this.roadManager = RoadManager.getInstance();
        this.skeletalAnimation = this.getComponent(SkeletalAnimation);
        this.animState = this.skeletalAnimation.getState('Take 001');
        this.playerManager.gameover = this.gameOver;
    }

    protected update(dt: number): void {
        if (this.bool) {
            let playerPosition = this.node.getWorldPosition();
            let PlayerVec3 = this.playerController.savePlayerVec3;
            let offsetx = Math.abs(playerPosition.x - PlayerVec3.x);
            if (offsetx < 0.1) {
                this.playerController.stopMovement();
                this.roadManager.setRoadName();
                this.roadManager.setIsMoving(false);
                this.bool = false;
            }
        }
    }

    // 碰撞开始时触发
    onCollisionEnter(event) {
        // 存储速度
        let currentVelocity = new Vec3();
        // 获取当前的速度
        this.rigidBody.getLinearVelocity(currentVelocity);
        if (this.playerManager) this.playerManager.jump = false;
        this.setAnimation();
        if (!this.rigidBody.group || !event.otherCollider.node.getComponent(RigidBody) || !event.otherCollider.node.getComponent(RigidBody).group) return;
        let group = event.otherCollider.node.getComponent(RigidBody).group;
        //如果角色碰到的是墙体障碍
        console.log(group);
        
        if (group == 2) {
            if (this.roadManager.isMoving) {
                let name = this.roadManager.cloneName;

                this.roadManager.setRoadName(name);
                this.bool = true;
            }
            this.getComponent(SkeletalAnimation).pause();

            setTimeout(() => {
                this.getComponent(SkeletalAnimation).play();

                if (currentVelocity.z < 26) {
                    this.gameover();
                } else {
                    this.playerManager.jump = false;
                }
            }, 500);


        } else if (group == 32) {
            //如果角色碰到的是地面
            this.playerManager.jump = false;
            this.playerManager.floor = event.otherCollider.node;

            let groupClone = null;
            let otherColliderY = event.otherCollider.node.getPosition().y;
            if (group !== groupClone) {
                groupClone = group;
                let posy = otherColliderY;

                this.node.parent.parent.emit('OffsetY', posy);
            }
        } else if (group == 4) {
            //如果角色碰到的是致命障碍
            this.gameover();
        }


    }

    gameover() {
        this.playerManager.GameOverFunction();
    }

    setAnimation() {
        if (this.skeletalAnimation) {
            if (this.animState && this.animState.isPlaying) {
            } else {
                this.getComponent(SkeletalAnimation).play();
            }
        }
    }

    onCollisionEnd(e) {

    }
}